<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
// // 用法如下:
function fn1(x) {
  return x + 1;
}
function fn2(x) {
  return x + 2;
}
function fn3(x) {
  return x + 3;
}
function fn4(x) {
  return x + 4;
}
const a = compose(fn1, fn2, fn3, fn4);
console.log(a(1)); // 1+4+3+2+1=11



function compose (...args) {
    let funcArray = [ ...args ]
    return function (value) {
        let res
        console.log('value', value)
        // funcArray.forEach((item) => {
        //     console.log('foreach:', item)
        //     console.log('typeof item:', typeof item)
        //     if (typeof item === 'function') {
        //         res = item(res)
        //     }
        // })
        res = funcArray.reduce((res, func) => {
            console.log(res)
            return func(res)
        }, value)
        return res
    }
}


</script>
</body>
</html>